package com.deve.pig.service.merchant.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import my.utils.Pagination;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.deve.pig.common.BusinessData;
import com.deve.pig.mapper.MerchantMapper;
import com.deve.pig.model.Category;
import com.deve.pig.model.Merchant;
import com.deve.pig.model.MerchantExample;
import com.deve.pig.service.merchant.IMerchantService;

@Service("merchantService")
public class MerchantServiceImpl extends BusinessData implements IMerchantService {
    @Resource
    private MerchantMapper merchantMapper;

    @Override
    public int add(Merchant entity) {
        return merchantMapper.insert(entity);
    }

    @Override
    public int delete(MerchantExample example) {
        return merchantMapper.deleteByExample(example);
    }

    @Override
    public int delete(int id) {
        return merchantMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(Merchant entity) {
        return merchantMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public Pagination findPagination(Map<String, Object> queryMap, int currentPage, int pageSize) {
        MerchantExample merchantExample = new MerchantExample();
        MerchantExample.Criteria criteria = merchantExample.createCriteria();
//         设置搜索条件参数
        if(queryMap != null){
            if(queryMap.containsKey("username")) {
                }
            if(queryMap.containsKey("email")){
                }
            
            if(queryMap.containsKey("is_publish")){
            	int is_publish = Integer.parseInt((String)queryMap.get("is_publish"));
            	if(is_publish == 1){
            		criteria.andIsPublishEqualTo((byte)1);
            	}else{
            		criteria.andIsPublishEqualTo((byte)0);
            	}
            }
            
            if(queryMap.containsKey("cateList")){
            	List<Category> cateList = (List<Category>) queryMap.get("cateList");
            	List<Integer> values = new ArrayList<Integer>();
            	for (Category category : cateList) {
            		values.add(category.getId());
				}
            	criteria.andCategoryIdIn(values);
            	if(queryMap.containsKey("keyword")){
                	String keyword = queryMap.get("keyword") ==null?"":(String)queryMap.get("keyword");
                	merchantExample.or().andNameLike("%"+keyword+"%");
                }
            }
            
            if(queryMap.containsKey("keyword")){
            	if(!queryMap.containsKey("cateList")){
            		String keyword = queryMap.get("keyword") ==null?"":(String)queryMap.get("keyword");
            		criteria.andNameLike("%"+keyword+"%");
            	}
            }
            
            if(queryMap.containsKey("latestpublish")){
            	boolean isLatest = Boolean.parseBoolean((String)queryMap.get("latestpublish"));
            	if(isLatest){
            		merchantExample.setOrderByClause("publish_time desc");
            	}
            }
        }
        // 设置分页参数
        merchantExample.setPageSize(pageSize);
        merchantExample.setStartIndex((currentPage-1)*pageSize);
        List<Merchant> items = merchantMapper.selectByExample(merchantExample);
        int totalCount = (int)merchantMapper.countByExample(merchantExample);
        return new Pagination(pageSize, currentPage, totalCount, items);
    }

    @Override
    public Merchant findById(int id) {
        return merchantMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Merchant> findAll() {
        return merchantMapper.selectByExample(null);
    }

    @Override
    public List<Merchant> findByCriteria(MerchantExample criteria) {
        return merchantMapper.selectByExample(criteria);
    }

    @Override
    @SuppressWarnings("unchecked")
    public LinkedHashMap<String, List> getExportData() {
        LinkedHashMap<String, List> map = new LinkedHashMap<String, List>();
        map.put("SheetName_1", findAll());
        map.put("SheetName_2", findAll());
        map.put("SheetName_3", findAll());
        map.put("SheetName_N", findAll());
        return map;
    }

    @Override
    public int countByExample(MerchantExample example) {
        return merchantMapper.countByExample(example);
    }
}